package james.gan.remoting.netty;

import java.net.SocketAddress;

import org.apache.commons.pool2.impl.GenericObjectPool;

import james.gan.common.Request;
import james.gan.remoting.api.Client;
import james.gan.remoting.api.RemotingException;
import james.gan.remoting.api.RemotingFutureDispatchMonitor.RemotingFuture;
import james.gan.remoting.netty.pool.NettyClientPool;
import james.gan.remoting.netty.pool.NettyClientProxy;
import james.gan.serialization.api.Serialization;

public class NettyClient implements Client {
	
	private Serialization serializer;
	
	public NettyClient(Serialization serializer) {
		super();
		this.serializer = serializer;
	}

	@Override
	public RemotingFuture send(SocketAddress address, Request request) throws RemotingException {
		GenericObjectPool<NettyClientProxy> clientPool = NettyClientPool.getWithSocketAddress(address, serializer);
		try {
			NettyClientProxy borrowObject = clientPool.borrowObject();
			return borrowObject.send(request);
		} catch (Exception e) {
			throw new RemotingException(e);
		}
	}
	
}